﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;

/// <summary>
/// 工具类
/// </summary>
internal class CUtility
{
    /// <summary>
    /// 获取当前应用的决对路径
    /// </summary>
    /// <returns></returns>
    public static string GetBaseDirectory()
    {
        return AppDomain.CurrentDomain.BaseDirectory;
    }

    /// <summary>
    /// 根据表名，算出类名
    /// </summary>
    /// <param name="tablename"> 如： t_users </param>
    /// <returns> Users </returns>
    public static string GetTableName(string tablename)
    {
        string first = "";  // 首字母

        tablename = tablename.Replace("t_", "").Replace("T_", "");
        if (tablename.Equals(""))
            return tablename;

        if (tablename.Contains("_"))
        {
            string str = "";
            string[] strlist = tablename.Split('_');

            tablename = "";
            for (int i = 0; i < strlist.Length; i++)
            {
                str = strlist[i];
                first = str.Substring(0, 1);
                first = first.ToUpper();

                tablename += first + str.Substring(1, str.Length - 1);
            }
        }
        else
        {
            first = tablename.Substring(0, 1);
            first = first.ToUpper();
            tablename = first + tablename.Substring(1, tablename.Length - 1);
        }

        return tablename;
    }

    /// <summary>
    /// sql数据类型转换为C#数据类型
    /// </summary>
    /// <param name="typename"></param>
    /// <returns></returns>
    public static string SqlDataTypeToCodeType(string typename)
    {
        typename = typename.Trim().ToLower();
        string codeTypeName = typename;

        switch (typename)
        {
            case "int":
            case "int identity": 
                codeTypeName = "int";
                break;
            case "bigint":
            case "bigint identity":
                codeTypeName = "long";
                break;
            case "tinyint":
                codeTypeName = "byte";
                break;
            case "smallint":
                codeTypeName = "short";
                break;
            case "char":
            case "nchar":
            case "varchar":
            case "nvarchar":
            case "text":
            case "ntext":
            case "xml":
                codeTypeName = "string";
                break;
            case "float":
                codeTypeName = "float";
                break;
            case "double":
                codeTypeName = "double";
                break;
            case "smalldatetime":
            case "datetime":
                codeTypeName = "DateTime";
                break;
            case "bit":
                codeTypeName = "bool";
                break;
            case "decimal":
            case "momey":
            case "numeric":
            case "smallmoney":
                codeTypeName = "decimal ";
                break;
        }

        return codeTypeName;
    }

    /// <summary>
    /// 根据表名查询出该表的主键
    /// </summary>
    /// <param name="tablename"></param>
    /// <param name="db"></param>
    /// <returns></returns>
    public static string GetKeyNameByTableName(string tablename, CDatabase db)
    {
        string keyName = "";        // 主键名

        string sql = "sp_COLUMNS '" + tablename + "'";
        DataTable dt_columns = db.ExecuteSql(sql);

        if (dt_columns != null && dt_columns.Rows.Count > 0)
        {
            DataRow[] dr = dt_columns.Select("ordinal_position=1");
            if (dr.Length > 0)
                keyName = dr[0]["column_name"].ToString();
        }

        return keyName;
    }

    /// <summary>
    /// 获取字段说明
    /// </summary>
    /// <param name="tablename"> 表名 </param>
    /// <param name="columnName"> 字段名 </param>
    /// <returns></returns>
    public static string GetColumnDescription(string tablename, string columnName, CDatabase db)
    {
        string Description = "";

        string sql = "select PT.value as description from syscolumns COL";
        sql += " left join sys.extended_properties PT on COL.id=PT.major_id and COL.colid=PT.minor_id ";
        sql += " where col.id = (select OBJECT_ID('" + tablename + "')) and COL.name = '" + columnName + "' ";

        DataTable dt_columns = db.ExecuteSql(sql);

        if (dt_columns != null && dt_columns.Rows.Count > 0)
        {
            Description = dt_columns.Rows[0]["description"].ToString();
        }

        if (Description == "")
            Description = columnName;

        return Description;
    }



}
